#include "WS2812.h"

uint8_t WS2812_LED_marquee_Flag ;//跑马灯效标志

uint16_t WS2812_LED_Out_Buffer[24 * WS2812_LED_Number] = {0};//灯效Buffer缓存数组

WS2812_LED_RGB_TypeInit WS2812_LED_RGB_homochromy_Value;//同色结构体

WS2812_LED_RGB_Lighting_Effects_Init WS2812_LED_RGB_heterochrome_Value;//异色结构体

/*所有灯同色*/
void WS2812_PWM_Out_Compar_Buffer_Config(WS2812_LED_RGB_TypeInit * WS2812_LED_RGB_Value)
{
	uint8_t temp_LED_Color_R;
	uint8_t temp_LED_Color_G;
	uint8_t temp_LED_Color_B;

	uint8_t temp_LED_Number;//灯数变量缓存
	uint8_t temp_LED_Skewing;//LED灯效偏移变量


	for(temp_LED_Number = 0;temp_LED_Number < WS2812_LED_Number;temp_LED_Number ++)
	{
			//结构体灯效写入缓存变量
			temp_LED_Color_R = WS2812_LED_RGB_Value->Color_R;
			temp_LED_Color_G = WS2812_LED_RGB_Value->Color_G;
			temp_LED_Color_B = WS2812_LED_RGB_Value->Color_B;

		for(temp_LED_Skewing = 0;temp_LED_Skewing < 8;temp_LED_Skewing++)
		{
			if(temp_LED_Color_R & 0x80)
			{
				WS2812_LED_Out_Buffer[temp_LED_Number*24 + temp_LED_Skewing+WS2812_LED_R_BASE] = WS2812_1_CODE;
			}
			else
			{
				WS2812_LED_Out_Buffer[temp_LED_Number*24 + temp_LED_Skewing+WS2812_LED_R_BASE] = WS2812_0_CODE;
			}
			if(temp_LED_Color_G & 0x80)
			{
				WS2812_LED_Out_Buffer[temp_LED_Number*24 + temp_LED_Skewing+WS2812_LED_G_BASE] = WS2812_1_CODE;
			}
			else
			{
				WS2812_LED_Out_Buffer[temp_LED_Number*24 + temp_LED_Skewing+WS2812_LED_G_BASE] = WS2812_0_CODE;
			}
			if(temp_LED_Color_B & 0x80)
			{
				WS2812_LED_Out_Buffer[temp_LED_Number*24 + temp_LED_Skewing+WS2812_LED_B_BASE] = WS2812_1_CODE;
			}
			else
			{
				WS2812_LED_Out_Buffer[temp_LED_Number*24 + temp_LED_Skewing+WS2812_LED_B_BASE] = WS2812_0_CODE;
			}
			temp_LED_Color_R<<=1;
			temp_LED_Color_G<<=1;
			temp_LED_Color_B<<=1;
		}
	}
}
/*所有灯同色结构体清零*/
void WS2812_PWM_Out_Compar_Buffer_Config_Reset(WS2812_LED_RGB_TypeInit * WS2812_LED_RGB_Value)
{
	WS2812_LED_RGB_Value->Color_R = 0;
	WS2812_LED_RGB_Value->Color_G = 0;
	WS2812_LED_RGB_Value->Color_B = 0;
}

/*所有灯不同色*/
void WS2812_Lighting_Effects_Config(WS2812_LED_RGB_Lighting_Effects_Init * WS2812_LED_RGB_Value)
{
	uint8_t temp_LED_Color_R;
	uint8_t temp_LED_Color_G;
	uint8_t temp_LED_Color_B;

	uint8_t temp_LED_Number;//
	uint8_t temp_LED_Skewing;//

	for(temp_LED_Number = 0;temp_LED_Number < WS2812_LED_Number;temp_LED_Number ++)
	{
			temp_LED_Color_R = WS2812_LED_RGB_Value->Color_R[temp_LED_Number];
			temp_LED_Color_G = WS2812_LED_RGB_Value->Color_G[temp_LED_Number];
			temp_LED_Color_B = WS2812_LED_RGB_Value->Color_B[temp_LED_Number];

		for(temp_LED_Skewing = 0;temp_LED_Skewing< 8;temp_LED_Skewing++)
		{
			if(temp_LED_Color_R & 0x80)
			{
				WS2812_LED_Out_Buffer[temp_LED_Number*24 + temp_LED_Skewing+WS2812_LED_R_BASE] = WS2812_1_CODE;
			}
			else
			{
				WS2812_LED_Out_Buffer[temp_LED_Number*24 + temp_LED_Skewing+WS2812_LED_R_BASE] = WS2812_0_CODE;
			}
			if(temp_LED_Color_G & 0x80)
			{
				WS2812_LED_Out_Buffer[temp_LED_Number*24 + temp_LED_Skewing+WS2812_LED_G_BASE] = WS2812_1_CODE;
			}
			else
			{
				WS2812_LED_Out_Buffer[temp_LED_Number*24 + temp_LED_Skewing+WS2812_LED_G_BASE] = WS2812_0_CODE;
			}
			if(temp_LED_Color_B & 0x80)
			{
				WS2812_LED_Out_Buffer[temp_LED_Number*24 + temp_LED_Skewing+WS2812_LED_B_BASE] = WS2812_1_CODE;
			}
			else
			{
				WS2812_LED_Out_Buffer[temp_LED_Number*24 + temp_LED_Skewing+WS2812_LED_B_BASE] = WS2812_0_CODE;
			}
			temp_LED_Color_R<<=1;
			temp_LED_Color_G<<=1;
			temp_LED_Color_B<<=1;
		}
	}
}

/*所有灯不同色结构体清零*/
void WS2812_Lighting_Effects_Config_Reset(WS2812_LED_RGB_Lighting_Effects_Init * WS2812_LED_RGB_Value)
{
	uint8_t for_Temp;
	for(for_Temp = 0;for_Temp < WS2812_LED_Number;for_Temp ++)
	{
		WS2812_LED_RGB_Value->Color_R[for_Temp] = 0;
		WS2812_LED_RGB_Value->Color_G[for_Temp] = 0;
		WS2812_LED_RGB_Value->Color_B[for_Temp] = 0;
	}
}

/*左跑马灯*/
void Left_Run_Led(WS2812_LED_RGB_TypeInit * RGB_Color,uint8_t Mode)
{
	static uint8_t Left_Flag = (WS2812_LED_Number);
	
	static uint8_t for_Temp;

	static uint8_t temp_LED_Color_R;
	static uint8_t temp_LED_Color_G;
	static uint8_t temp_LED_Color_B;

	if(WS2812_LED_marquee_Flag == LED_Left)
    {
	  for(for_Temp = 0;for_Temp < WS2812_LED_Number;for_Temp ++)
	  {
		if(for_Temp == ( Left_Flag-1) )
		{
			temp_LED_Color_R = RGB_Color -> Color_R;
			temp_LED_Color_G = RGB_Color -> Color_G;
			temp_LED_Color_B = RGB_Color -> Color_B;
		}
		else
		{
			temp_LED_Color_R = 0;
			temp_LED_Color_G = 0;
			temp_LED_Color_B = 0;
		}

		WS2812_LED_RGB_heterochrome_Value.Color_R[for_Temp] = temp_LED_Color_R;
		WS2812_LED_RGB_heterochrome_Value.Color_G[for_Temp] = temp_LED_Color_G;
		WS2812_LED_RGB_heterochrome_Value.Color_B[for_Temp] = temp_LED_Color_B;

	  }//for_end
		
		//将灯效数据写入Buff数组内
        WS2812_Lighting_Effects_Config(&WS2812_LED_RGB_heterochrome_Value);
if(Mode == Circulation)
{
		Left_Flag --;
	
		if(( Left_Flag ) == 0)
		{
			Left_Flag = (WS2812_LED_Number);
		}
}

if(Mode == Single)
{
		if(Left_Flag != 0)
		{
			Left_Flag --;
		}
		else
		{
			WS2812_Lighting_Effects_Config_Reset(&WS2812_LED_RGB_heterochrome_Value);
			WS2812_Lighting_Effects_Config(&WS2812_LED_RGB_heterochrome_Value);
		}
}
    }//if_end

}
/*右跑马灯*/
void Right_Run_Led(WS2812_LED_RGB_TypeInit * RGB_Color,uint8_t Mode)
{
	static uint8_t Right_Flag = 0;

	uint8_t for_Temp;

	uint8_t temp_LED_Color_R;
	uint8_t temp_LED_Color_G;
	uint8_t temp_LED_Color_B;

	if(WS2812_LED_marquee_Flag == LED_Right)
    {
	  for(for_Temp = 0;for_Temp < WS2812_LED_Number;for_Temp ++)
	  {
		if(for_Temp == Right_Flag)
		{
			temp_LED_Color_R = RGB_Color ->Color_R;
			temp_LED_Color_G = RGB_Color ->Color_G;
			temp_LED_Color_B = RGB_Color ->Color_B;
		}
		else
		{
			temp_LED_Color_R = 0;
			temp_LED_Color_G = 0;
			temp_LED_Color_B = 0;
		}

		WS2812_LED_RGB_heterochrome_Value.Color_R[for_Temp] = temp_LED_Color_R;
		WS2812_LED_RGB_heterochrome_Value.Color_G[for_Temp] = temp_LED_Color_G;
		WS2812_LED_RGB_heterochrome_Value.Color_B[for_Temp] = temp_LED_Color_B;

	  }

		//将灯效数据写入Buff数组内
        WS2812_Lighting_Effects_Config(&WS2812_LED_RGB_heterochrome_Value);

if(Mode == Circulation)
{
          if(( Right_Flag ++ ) == 7)
          {
            Right_Flag = 0;
          }
}

if(Mode == Single)
{
		  if(Right_Flag != WS2812_LED_Number)
		  {
			Right_Flag ++;
		  }
		  else
		  {
			WS2812_Lighting_Effects_Config_Reset(&WS2812_LED_RGB_heterochrome_Value);
			WS2812_Lighting_Effects_Config(&WS2812_LED_RGB_heterochrome_Value);
		  }
}
    }//if_end

}
/*
void Location_LED(uint8_t * RX_Buf)
{
	uint8_t for_Temp;

	uint8_t temp_LED_Color_R;
	uint8_t temp_LED_Color_G;
	uint8_t temp_LED_Color_B;

	  for(for_Temp = 0;for_Temp < WS2812_LED_Number;for_Temp ++)
	  {
		if(for_Temp == (*RX_Buf))
		{
			temp_LED_Color_R = 128;
			temp_LED_Color_G = 0;
			temp_LED_Color_B = 0;
		}
		else
		{
			temp_LED_Color_R = 0;
			temp_LED_Color_G = 0;
			temp_LED_Color_B = 0;
		}

		WS2812_LED_RGB_Value.Color_R[for_Temp] = temp_LED_Color_R;
		WS2812_LED_RGB_Value.Color_G[for_Temp] = temp_LED_Color_G;
		WS2812_LED_RGB_Value.Color_B[for_Temp] = temp_LED_Color_B;

	  }

        WS2812_Lighting_Effects_Config(&WS2812_LED_RGB_Value);
		HAL_TIM_PWM_Start_DMA(&htim3, TIM_CHANNEL_2, (uint32_t *)WS2812_LED_Out_Buffer,sizeof(WS2812_LED_Out_Buffer)/sizeof(WS2812_LED_Out_Buffer[0] + 1));
		Get_LED_msm = LED_Close;
}
*/
